package cn.wolfcode.shop.cloud.mq;

import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@RocketMQMessageListener(

        consumerGroup = MQConstans.CREATE_ORDER_FAIL_GROUP,

        topic = MQConstans.CREATE_ORDER,

        selectorExpression = MQConstans.CREATE_ORDER_FAIL_TAG

)

@Component

public class OrderFailMQListener implements RocketMQListener<Map> {

    @Autowired

    private RocketMQTemplate rocketMQTemplate;

    @Autowired

    private ISeckillGoodService seckillGoodService;

    @Override

    public void onMessage(Map  map) {
        System.out.println("进入到失败的消费者");

        Object seckillId =  map.get("seckillId");
        Long goodId=null;

        if(seckillId instanceof  Long){

            goodId=(Long)seckillId;

        }else if(seckillId instanceof  Integer){

            goodId=((Integer) seckillId).longValue();

        }
        //回补预库存  seckill_stock:11
        seckillGoodService.syncPreCount(goodId);
        //修改本地标识
        rocketMQTemplate.syncSend(MQConstans.CREATE_ORDER_BROAD_DEST,goodId);

    }

}